package com.example.exceldemo.utils;

import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


public class ImportExcelUtil {
	
	private final static String Excel_2003 = ".xls";
	private final static String Excel_2007 = ".xlsx";
	

	
	/**
	 * @description 解析Excel
	 * @param in
	 * @param fileName
	 * @return
	 * @throws Exception
	 * @throws IOException
	 */
	@SuppressWarnings("unlikely-arg-type")
	public static  List<List<Object>> getBankListByExcel(InputStream in,String fileName) throws Exception, IOException{
		List<List<Object>> list = null;
		Workbook work = getWorkbook(in, fileName);
		if(work == null){
			throw new Exception("创建Excel工作簿为空！");
		}
		Sheet sheet = null;
		Row row = null;
		Cell cell = null;
		
		list = new ArrayList<List<Object>>();
		for(int i = 0;i< work.getNumberOfSheets();i++){
			sheet = work.getSheetAt(i);
			if(sheet == null){
				continue;
			}
			int totalRow = sheet.getPhysicalNumberOfRows();
			for(int j = sheet.getFirstRowNum();j < totalRow;j++){
				row = sheet.getRow(j);
				if(isRowEmpty(row))
					continue;
				if(row != null && !"".equals(row)){
					Cell fristCell = row.getCell(0);
					if(fristCell != null){
						List<Object> li = new ArrayList<>();
						for(int y = row.getFirstCellNum(); y < row.getLastCellNum(); y++){
							cell = row.getCell(y);
							String callCal = getCellValue(cell) + "";
							li.add(callCal);
						}
						list.add(li);
					}
				}
			}
		}
		in.close();
		return list;
	}

	public static Workbook getWorkbook(InputStream in,String fileName) throws Exception, IOException{
		Workbook work = null;
		String fileType = fileName.substring(fileName.lastIndexOf('.'));
		if(Excel_2003.equals(fileType)){
			work = new HSSFWorkbook(in);
		}else if(Excel_2007.equals(fileType)){
			work = new XSSFWorkbook(in);
		}else{
			throw new Exception("解析文件格式有误");
		}
		
		return work;
	}
	
	@SuppressWarnings("unlikely-arg-type")
	public static Object getCellValue(Cell cell){
		
		Object value = null;
//		DecimalFormat df1 = new DecimalFormat("0");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//日期格式化
//		DecimalFormat df2 = new DecimalFormat("0.00");
		if(cell != null && !"".equals(cell)){
			switch (cell.getCellType()) {
				case Cell.CELL_TYPE_STRING:
					value = cell.getRichStringCellValue();
					break;
				case Cell.CELL_TYPE_NUMERIC:
					if("General".equals(cell.getCellStyle().getDataFormatString())){
						value = cell.getNumericCellValue();
//						value = df2.format(cell.getNumericCellValue());
					}else if("m/d/yy".equals(cell.getCellStyle().getDataFormatString())){
						value = sdf.format(cell.getDateCellValue());
					}else if(HSSFDateUtil.isCellDateFormatted(cell)){
						Date date = cell.getDateCellValue();
						value = sdf.format(date);
					}else{
						value = cell.getNumericCellValue();
//						value = df2.format(cell.getNumericCellValue());
					}
					
//					value = cell.getRichStringCellValue();
					break;
				case Cell.CELL_TYPE_BOOLEAN:
					value = cell.getBooleanCellValue();
					break;
				case Cell.CELL_TYPE_BLANK:
					value = "";
					break;
				default:
					break;
			}
		}
		
		return value;
	}
	
	/**
	 * 
	 * @description 判断是否是空行
	 * @author cqw
	 * @date 2019年4月23日上午1:08:30
	 * @param row
	 * @return
	 */
	public static boolean isRowEmpty(Row row) {
		for (int c = row.getFirstCellNum(); c < row.getLastCellNum(); c++) {
			Cell cell = row.getCell(c);
			if (cell != null && cell.getCellType() != Cell.CELL_TYPE_BLANK)
				return false;
			}
			return true;
		}
	
	public String getFormat(String str){
		if(str.equals("null")){
			str = "";
			return str;
		}else{
			return str;
		}
		
	}
	
	public Integer getFormats(Integer str){
		if(str == null){
			str = 0;
			return str;
		}else{
			return str;
		}
	}
	
}
